package com.sdut.material.controller;


import com.sdut.material.pojo.Role;
import com.sdut.material.pojo.vo.LayUISelectMVO;
import com.sdut.material.service.IRoleService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @RequestMapping("/selectAllRoles")
    @ResponseBody
    public JSONResult selectAllRoles() {
       List<LayUISelectMVO> list =  roleService.selectAllRoles();
        return JSONResult.ok(list);
    }
    @RequestMapping("/getRoleListPage")
    public String getRoleListPage() {
        return "role/role_list";
    }
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(Long page,Long limit) {
        return roleService.selectByPage(page,limit);
    }
    @RequestMapping("/getRoleInsertPage")
    public String getRoleInsertPage() {
        return "role/role_insert";
    }
    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(Role role,String permissions) {
        return roleService.insert(role,permissions);
    }
    @RequestMapping("/getRoleUpdatePage")
    public String getRoleUpdatePage() {
        return "role/role_update";
    }
    @RequestMapping("/selectRoleAndPermissions")
    @ResponseBody
    public JSONResult selectRoleAndPermissions(Integer roleId) {
        return roleService.selectRoleAndPermissions(roleId);
    }
    @RequestMapping("/update")
    @ResponseBody
    public JSONResult updateRole(Role role,String permissions) {
        return roleService.updateRole(role,permissions);
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer roleId) {
        roleService.deleteById(roleId);
        return JSONResult.ok("删除成功");
    }
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(String ids) {
        String[] idArray = ids.split(",");
        for (String roleId : idArray) {
            deleteById(Integer.parseInt(roleId));
        }
        return JSONResult.ok("删除成功");
    }
}

